package com.l.im;

import com.l.im.common.config.ConfigReader;
import com.l.im.common.mq.RocketMQConsumer;
import com.l.im.mq.GroupMessageListener;
import com.l.im.mq.SingleMessageListener;
import com.l.im.server.IMServer;
import com.l.im.service.init.WebSocketServiceRegistry;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

/**
 * @program: LIM
 * @description: 启动类
 * @author: Alex Wu
 * @createDate: 2025-04-14 17:11
 **/

@Slf4j
public class IMBootStrap {
    public static void main(String[] args) {
        try {
            CuratorFramework client = CuratorFrameworkFactory.newClient(
                    ConfigReader.getInstance().getOrDefault("zookeeper.address", String.class, "localhost:2181"),
                    new ExponentialBackoffRetry(1000, 3)
            );
            client.start();

            WebSocketServiceRegistry registry = new WebSocketServiceRegistry(client, "localhost", ConfigReader.getInstance().getOrDefault("server.port", Integer.class, 8090));
            registry.register();

            RocketMQConsumer singleConsumer = new RocketMQConsumer();
            singleConsumer.start(
                    ConfigReader.getInstance().getOrDefault("rocketmq.endpoint" , String.class, "localhost:9876"),
                    ConfigReader.getInstance().getOrDefault("rocketmq.single-consumer-topic",String.class,""),
                    ConfigReader.getInstance().getOrDefault("rocketmq.single-consumer-topic-group",String.class,""),
                    "*",
                    new SingleMessageListener()
            );

            RocketMQConsumer groupConsumer = new RocketMQConsumer();
            groupConsumer.start(
                    ConfigReader.getInstance().getOrDefault("rocketmq.endpoint" , String.class, "localhost:9876"),
                    ConfigReader.getInstance().getOrDefault("rocketmq.group-consumer-topic",String.class,""),
                    ConfigReader.getInstance().getOrDefault("rocketmq.group-consumer-topic-group",String.class,""),
                    "*",
                    new GroupMessageListener()
            );

            log.info("IM服务启动..");
            // 启动IM服务
            new IMServer().start();

        }catch (Exception e) {
            e.printStackTrace();
        }

    }
}
